package com.qf.smsplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.webmaster.cache.NotifyCache;
import com.qf.smsplatform.webmaster.constans.ResultCode;
import com.qf.smsplatform.webmaster.events.UpdateNotifyEvent;
import com.qf.smsplatform.webmaster.exceptions.SelectException;
import com.qf.smsplatform.webmaster.mapper.NotifyMapper;
import com.qf.smsplatform.webmaster.pojo.MyAuthUser;
import com.qf.smsplatform.webmaster.pojo.TNotify;
import com.qf.smsplatform.webmaster.service.NotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class NotifyServiceImpl implements NotifyService {

    private NotifyMapper notifyMapper;

    private NotifyCache notifyCache;

    private ApplicationContext context;

    @Autowired
    public void setNotifyCache(NotifyCache notifyCache){
        this.notifyCache = notifyCache;
    }

    @Autowired
    public void setNotifyMapper(NotifyMapper notifyMapper){
        this.notifyMapper = notifyMapper;
    }

    @Autowired
    public void setContext(ApplicationContext context){
        this.context = context;
    }
    @Override
    public PageInfo<TNotify> showAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TNotify> allDate = notifyCache.getAllData();
        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user.getType() != 1) {
            throw new SelectException("你没有权限查看该数据", ResultCode.NOT_ALLOW_ACCESS);
        }

        List<TNotify> collect = allDate.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        PageInfo<TNotify> pageInfo = new PageInfo<>(collect);
        return pageInfo;
    }


    @Override//修改
    public void updateNotify(TNotify tNotify) {
        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user.getType() != 1) {
            throw new SelectException("你没有权限查看该数据", ResultCode.NOT_ALLOW_ACCESS);
        }
        notifyMapper.updateNotity(tNotify);
        context.publishEvent(new UpdateNotifyEvent());
    }

    @Override       //添加
    public void add(TNotify tNotify) {
        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user.getType() != 1) {
            throw new SelectException("你没有权限添加该数据", ResultCode.NOT_ALLOW_ACCESS);
        }
        notifyMapper.insert(tNotify);
        context.publishEvent(new UpdateNotifyEvent());
    }


    @Override   //删除
    public void delete(List<Long> id) {
        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user.getType() != 1) {
            throw new SelectException("你没有权限删除该数据", ResultCode.NOT_ALLOW_ACCESS);
        }
        notifyMapper.delete(id);
        context.publishEvent(new UpdateNotifyEvent());
    }
}
